//
// Created on 2024/8/6.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be
// found, please include "napi/native_api.h".

#ifndef ARKUI_C_API_TLANGTEXT_H
#define ARKUI_C_API_TLANGTEXT_H

#include "src/ArkUITextNode.h"
#include "../tlang/tvm/rt_code/NativeMethodTable.h"
#include "src/NativeEntry.h"
#include <memory>

using std::shared_ptr;

class TextWrapper {
    shared_ptr<NativeModule::ArkUITextNode> text;

public:
    TextWrapper() { text = std::make_shared<NativeModule::ArkUITextNode>(); }

    shared_ptr<NativeModule::ArkUITextNode> getTarget() { return text; }

    static void bind(shared_ptr<RtCode::NativeMethodTable> nativeMethodTable) {
        nativeMethodTable->add("Text", "create", create);
        nativeMethodTable->add("Text", "fontSize", setFontSize);
        nativeMethodTable->add("Text", "width", setWidth);
        nativeMethodTable->add("Text", "height", setHeight);
        nativeMethodTable->add("Text", "backgroundColor", setBackgroundColor);
        nativeMethodTable->add("Text", "render", render);
    }

    static void create(RtData::Frame &frame) {
        auto *textWrapper = new TextWrapper();
        auto arg = frame.popValue().value.int64;
        textWrapper->text->SetTextContent(std::to_string(arg));
        frame.pushRef(reinterpret_cast<RtData::Object *>(textWrapper));
    }

    static void setFontSize(RtData::Frame &frame) {
        auto *self = frame.getSlot(0).getRef();
        auto *textWrapper = reinterpret_cast<TextWrapper *>(self);
        auto arg = frame.popValue().value.int64;
        textWrapper->text->SetFontSize(arg);
    }

    static void setWidth(RtData::Frame &frame) {
        auto *self = frame.getSlot(0).getRef();
        auto *textWrapper = reinterpret_cast<TextWrapper *>(self);
        auto arg = frame.popValue().value.int64;
        textWrapper->text->SetWidth(arg);
    }

    static void setHeight(RtData::Frame &frame) {
        auto *self = frame.getSlot(0).getRef();
        auto *textWrapper = reinterpret_cast<TextWrapper *>(self);
        auto arg = frame.popValue().value.int64;
        textWrapper->text->SetHeight(arg);
    }

    static void setBackgroundColor(RtData::Frame &frame) {
        auto *self = frame.getSlot(0).getRef();
        auto *textWrapper = reinterpret_cast<TextWrapper *>(self);
        auto arg = frame.popValue().value.int64;
        textWrapper->text->SetBackgroundColor(arg);
    }

    static void render(RtData::Frame &frame) {
        auto *self = frame.getSlot(0).getRef();
        auto *textWrapper = reinterpret_cast<TextWrapper *>(self);
        shared_ptr<NativeModule::ArkUIBaseNode> text = textWrapper->text;
        NativeModule::NativeEntry::GetInstance()->SetRootNode(text);
    }
};

#endif // ARKUI_C_API_TLANGTEXT_H
